package com.itheima.bos.web.action;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;

import com.itheima.bos.domain.base.Courier;
import com.itheima.bos.domain.base.Standard;
import com.itheima.bos.service.CourierService;
import com.itheima.bos.web.action.common.BaseAction;

@Controller
@Scope("prototype")
@ParentPackage("json-default")
@Namespace("/courier")
public class CourierAction extends BaseAction<Courier> {

	@Autowired
	private CourierService courierService;

	@Action(value = "courier_save", results = {
			@Result(name = "success", type = "redirect", location = "../pages/base/courier.html") })
	public String courier_save() {
		courierService.courier_save(model);
		return SUCCESS;
	}
	
	@Action(value="courier_pageQuery", results={@Result(name="success",type="json")})
	public String courier_pageQuery(){
		Pageable pageable = new PageRequest(page - 1, rows);
		Specification<Courier> spec = new Specification<Courier>() {

			@Override
			public Predicate toPredicate(Root<Courier> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				Predicate predicate = null;
				List<Predicate> list = new ArrayList<Predicate>();
				if(StringUtils.isNotBlank(model.getCourierNum())){
					predicate = cb.equal(root.get("courierNum").as(String.class), model.getCourierNum());
					list.add(predicate);
				}
				if(StringUtils.isNotBlank(model.getCompany())){
					predicate = cb.like(root.get("company").as(String.class), "%" + model.getCompany() + "%");
					list.add(predicate);
				}
				if(StringUtils.isNotBlank(model.getType())){
					predicate = cb.like(root.get("type").as(String.class),"%" + model.getType() + "%");
					list.add(predicate);
				}
				if(model.getStandard() != null && model.getStandard().getId() != null){
					Join<Courier, Standard> join = root.join("standard",JoinType.INNER);
					predicate = cb.equal(join.get("id").as(Integer.class), model.getStandard().getId());
					list.add(predicate);
				}
				
				//如果条件存在!
				if(list != null && list.size() >= 1){
					Predicate p[] = new Predicate[list.size()];
					predicate = cb.and(list.toArray(p));
				}
				return predicate;
			}
		};

		Page<Courier> page = courierService.courier_pageQuery(spec, pageable);
		pushPageDateToValueStack(page);
		return SUCCESS;
	}
	
	private String ids;
	
	public void setIds(String ids) {
		this.ids = ids;
	}

	@Action(value="courier_delete",results={@Result(name="success",type="redirect",location="../pages/base/courier.html")})
	public String courier_delete(){
		courierService.delete(ids.split(","));
		return SUCCESS;
	}
	
	@Action(value="courier_findNoAssociation",results={@Result(name="success",type="json")})
	public String courier_findNoAssociation(){
		List<Courier> list = courierService.findNoAssociation();
		ServletActionContext.getContext().getValueStack().push(list);
		return SUCCESS;
	}
}
